<template>
	<view class="" style="padding-bottom: 150rpx;">
		<u-navbar title="消息" placeholder titleStyle="color:#fff" leftIconColor="#c1282e" bgColor="#c1282e">
			<view class="flexright xiaobai" slot="right" v-if="userInfo.is_zc==1 && tabsid==2"
				@click="jumpurl('/pages/tab5/createGroup')">创建群聊</view>
		</u-navbar>
		<u-tabs :list="tabslist" :scrollable="false" lineColor="#c1282e" @click="changetab" :activeStyle="{
		    color: '#c1282e',
		    fontWeight: 'bold',
		    transform: 'scale(1.05)'
		}" :inactiveStyle="{
		            color: '#333333',
					fontWeight: '540',
		            transform: 'scale(1)'
		        }" itemStyle="padding-left: 10px; padding-right: 10px; height: 44px;"></u-tabs>
		<!-- 私聊 -->
		<view class="mainpadding2" v-if="tabsid==1">
			<view class="chatItemMessage margin_top" v-for="item in chatList" :key="item.id"
				@longpress="handLongpress(item)" @click="enterchat(item)">
				<view class="imageBox">
					<image :src="item.firend.avatar_text" mode="">
					</image>
				</view>
				<view class="right">
					<view class="nameTime">
						<view class="flexleft">
							<view class="name">{{item.firend.username}}</view>
							<view class="number" v-if="item.noread_num">{{item.noread_num}}</view>
						</view>
						<view class="time" v-if="item.new_content">{{item.new_content.createtime_text}}</view>
					</view>
					<template v-if="item.new_content">
						<view class="bottom yihang" v-if="item.new_content.data_type==1">{{item.new_content.message}}
						</view>
						<view class="bottom yihang" v-if="item.new_content.data_type==2">[图片]</view>
						<view class="bottom yihang" v-if="item.new_content.data_type==3">[视频]</view>
						<view class="bottom yihang" v-if="item.new_content.data_type==4">[文件]</view>
					</template>
				</view>
			</view>
		</view>
		<!-- 群聊 -->
		<view class="mainpadding2" v-if="tabsid==2">
			<view class="chatItemMessage margin_top" v-for="item in qlchatList" :key="item.id" @click="enterchatgroup(item)">
				<view class="imageBox">
					<image :src="item.avatar" mode="">
					</image>
				</view>
				<view class="right">
					<view class="nameTime">
						<view class="flexleft">
							<view class="name">{{item.groupname}}</view>
							<!-- <view class="number" v-if="item.noread_num">{{item.noread_num}}</view> -->
						</view>
						<!-- <view class="time" v-if="item.new_content">{{item.new_content.createtime_text}}</view> -->
					</view>
					<!-- <view class="bottom yihang">{{item.sign}}</view> -->
					<template v-if="item.new_content">
						<view class="bottom yihang" v-if="item.new_content.data_type==1">{{item.new_content.message}}
						</view>
						<view class="bottom yihang" v-if="item.new_content.data_type==2">[图片]</view>
						<view class="bottom yihang" v-if="item.new_content.data_type==3">[视频]</view>
						<view class="bottom yihang" v-if="item.new_content.data_type==4">[文件]</view>
					</template>
				</view>
			</view>
		</view>
		<template v-if="tabsid==2">
			<liu-drag-button>
				<view class="addclass flexcolumn" @click="scancode">
					<u-icon name="scan" color="#ffffff" size="25"></u-icon>
					<!-- <view class="smalltext xiaobai">扫码加群</view> -->
				</view>
			</liu-drag-button>
		</template>
		
		<tabbar :current="4" ref="tabbarRef"></tabbar>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				user_id: "",
				chatList: [],
				qlchatList: [],
				websocket: null,
				tabsid: 1,
				tabslist: [{
					name: '私聊',
					value: 1,
				}, {
					name: '群聊',
					value: 2,
				}],
				userInfo: {},
			}
		},
		onLoad() {
			uni.hideTabBar()
			uni.$on("updatechatFnList", data => {
				console.log("触發方法了會话列表哈哈哈");
				this.init()
				this.initql()
			})
			this.getuserInfo()
		},
		onShow() {
			this.$nextTick(function() {
				this.$refs.tabbarRef.$emit("childmethod")
			});
			this.user_id = uni.getStorageSync("user_id")
			this.init()
			this.initql()
		},
		onUnload() {
			uni.$off('updatechatFnList')
		},
		methods: {
			// 长按事件
			handLongpress(item) {
				let that = this
				uni.showActionSheet({
					itemList: ['删除'],
					success: function(res) {
						if (res.tapIndex == 0) {
							httpRequest.request('/api/user/delChat', 'GET', {id:item.id}).then(resp => {
								if(resp.code==1){
									that.init()
								}
							})
						}
					},
					fail: function(res) {}
				});
			},
			changetab(e) {
				this.tabsid = e.value
			},
			init() {
				httpRequest.request('/api/user/getChatList', 'GET', {}).then(res => {
					this.chatList = res.data
				})
			},
			initql() {
				httpRequest.request('/webim/groups_user/getGroups', 'GET', {}).then(res => {
					this.qlchatList = res.data
				})
			},
			getuserInfo() {
				httpRequest.request('/api/user/getUerInfo', 'GET', {}).then(res => {
					this.userInfo = res.data
				})
			},
			enterchat(item) { //进入聊天
				let data = {
					firend_id: item.firend_id,
					friend_name: item.firend.username,
					firend_avatar_text: item.firend.avatar_text
				}
				uni.navigateTo({
					url: "/pages/tab5/chat?msg=" + encodeURIComponent(JSON.stringify(data))
				})
			},
			scancode(){
				// #ifdef APP-PLUS
				uni.scanCode({
					success: function(res) {
						// console.log('条码类型：' + res.scanType);
						// console.log('条码内容：' + res.result);
						httpRequest.request("/webim/message_box/addGroup", 'GET', {
							groups_id:res.result
						}, false, false, true).then(res => {
							httpRequest.toast(res.msg)
						})
					}
				});
				// #endif
				// #ifdef H5
				httpRequest.request("/webim/message_box/addGroup", 'GET', {
					groups_id:3
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
				})
				// #endif
			},
			enterchatgroup(item){//群聊
				let data = {
					group_id: item.id,
					groupname: item.groupname,
				}
				uni.navigateTo({
					url: "/pages/tab5/chatgroup?msg=" + encodeURIComponent(JSON.stringify(data))
				})
			},
			jumper(url) {
				uni.navigateTo({
					url: url
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.addclass {
		// width: 120rpx;
		// height: 120rpx;
		width: 86rpx;
		height: 86rpx;
		background-color: #c1282e;
		border-radius: 50%;
	}
	// 聊天消息
	.chatItemMessage {
		display: flex;

		.imageBox {
			width: 114rpx;
			min-width: 114rpx;
			height: 114rpx;
			border-radius: 50%;
			overflow: hidden;
			margin-right: 40rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.right {
			flex: 1;
		}

		.nameTime {
			display: flex;
			justify-content: space-between;

			.name {
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333;
				margin-right: 10rpx;
			}

			.number {
				width: 34rpx;
				height: 34rpx;
				line-height: 34rpx;
				text-align: center;
				background: #FF526A;
				border-radius: 50%;
				font-size: 20rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
			}

			.time {
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #666666;
			}
		}

		.bottom {
			font-size: 26rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #999999;
			margin-top: 35rpx;
		}
	}

	.tiaos {
		width: 34rpx;
		height: 34rpx;
		background: #FF526A;
		border-radius: 50%;
		font-size: 20rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}

	.touxiang {
		width: 98rpx;
		height: 98rpx;
		border-radius: 50%;
	}

	.iconxx {
		width: 88rpx;
		height: 88rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>